package com.coffee.tenant;

import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * this is web
 * mq, other thead   ==
 * @author songkui
 * @since 2024/4/17
 */
public class TenantContextWebFilter extends OncePerRequestFilter {
    private final CoffeeWebTenantDecider tenantDecider;
    public TenantContextWebFilter(CoffeeWebTenantDecider tenantDecider){
        this.tenantDecider = tenantDecider;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)throws ServletException, IOException {
        try {
            tenantDecider.decide(request);
            chain.doFilter(request, response);
        } finally {
            TenantContextHolder.clear();
        }

    }



}
